import LXCOM from '../common/common.js'
import TerminalGraph from '../graph/terminal.js'
import UtilFuncs from '../common/tools/utilfuncs.js'
var funcs = UtilFuncs.UtilFuncs;
import GraphFactory from '../graph/graphfactory.js'
import { LGXEnums } from '../common/sysenum.js';


import globalConst from '../common/globalConst.js';
var global = globalConst.GlobalConst;

import { GlobalVar } from '../common/globalvar.js';
import absgraph from '../graph/absgraph.js';

class LGXSymbol {
    constructor(type,symUUID, state) {
        this.uuid = symUUID;
        this.m_typeID = type;
        this.m_GraphUnitLst = new Array();
        this.m_symbolUnitLst = new Array();
        this.m_state = state;
        this.m_initialScopeFlag = false;
        this.m_symScope = new LXCOM.LGXRect();
        this.m_name = "";
        this.m_moldActionType = LGXEnums.moldActionType.common;
        //当模具为容器类型时，此字段标识容器底部是否跟容器子项对齐，如果是则每增加一个子项，就自动调整容器底部坐标
        this.m_algin2ItemBottom = false;
        this.exflag = false;//外部图符标识，如从dxf svg中导入的图符。此类图符随文件一起保存
        this.beenRefFlag = false;//被引用标识，被引用的随文件一起保存
    }

    addGraph(graph) {
        this.m_GraphUnitLst.push(graph);
    }

    display(attachGraph,transformInfo, map, mapInfo, forClip) {
        if (!mapInfo.paintForGenerateICON) {
            let hideCount = 0;
            let selFlag = attachGraph.getSelect();
            let len = this.m_GraphUnitLst.length;
            for (let i = 0; i < len; i++) {
                let unitGraph = this.m_GraphUnitLst[i];
                unitGraph.setSelect(selFlag);
                if (unitGraph.chkSymUnitInViewPort(mapInfo, transformInfo)) {
                    unitGraph.disInSymbol(transformInfo, map, mapInfo, forClip);
                }
                else {
                    hideCount++;
                }
                let counter = new absgraph.LineUnitCounter();
                counter.lineUnitCount++;
            }
        }
        else {
            GlobalVar.disLineWidth = true;
            let blackLineInfo = new LXCOM.LGXLineDefInfo();
            let len = this.m_GraphUnitLst.length;
            for (let i = 0; i < len; i++) {
                let unitGraph = this.m_GraphUnitLst[i];
                let preLineInfo = null;
                if (funcs.isBaseUnit(unitGraph.getType())) {
                    preLineInfo = new LXCOM.LGXLineDefInfo();
                    blackLineInfo.clone(unitGraph.getLineDef());
                    preLineInfo.clone(blackLineInfo);
                    blackLineInfo.color.setVal(0, 0, 0);
                    // blackLineInfo.lineWidth = mapInfo.paintForGenerateLineWidth;
                    unitGraph.setLineDef(blackLineInfo);
                }
                console.log(unitGraph);
                unitGraph.disInSymbol(transformInfo, map, mapInfo, forClip);
                if (preLineInfo) {
                    unitGraph.setLineDef(preLineInfo);
                }
            }
            GlobalVar.disLineWidth = false;
        }
    }

    addSymbolUnit(symbolInfo) {
        this.m_symbolUnitLst.push(symbolInfo);
    }

    endInitSymbol() {
        let len = this.m_symbolUnitLst.length;
        for (let i = 0; i < len; i++) {
            let obj = this.m_symbolUnitLst[i];
            var devFullType = obj.type;
            var pNewGraph = this.createUnitGraph(devFullType);
            if (pNewGraph) {
                try {
                    pNewGraph.assignInfo(obj);
                }
                catch (error) {
                    console.log(error);
                }
                // finally {
                //     console.log(obj);
                // }
                // if (funcs.isBaseUnit(pNewGraph.getType())) {
                //     let lineInfo = pNewGraph.getLineDef();
                //     let backcolor = GlobalVar.backcolor;
                //     let lineColor = lineInfo.color;
                //     let detaR = Math.abs(backcolor.red - lineColor.red);
                //     let detaG = Math.abs(backcolor.green - lineColor.green);
                //     let detaB = Math.abs(backcolor.blue - lineColor.blue);
                //     if (detaR < 10 && detaG < 10 && detaB < 10) {
                //         lineInfo.color.clone(backcolor.invert());
                //         obj.color = lineInfo.color.toValue();
                //     }
                // }
                // else if (pNewGraph.m_graphType == LGXEnums.LGXGraphType.TextGraph_type) {
                //     let fontInfo = pNewGraph.getFontInfo();
                //     let backcolor = GlobalVar.backcolor;
                //     let lineColor = fontInfo.fontColor;
                //     let detaR = Math.abs(backcolor.red - lineColor.red);
                //     let detaG = Math.abs(backcolor.green - lineColor.green);
                //     let detaB = Math.abs(backcolor.blue - lineColor.blue);
                //     if (detaR < 10 && detaG < 10 && detaB < 10) {
                //         fontInfo.fontColor.clone(backcolor.invert());
                //         obj.color = fontInfo.fontColor.toValue();
                //     }
                // }
                pNewGraph.calScope(1);
                this.addGraph(pNewGraph);
            }
        }
        this.calSymbolScope();
    }

    calSymbolScope() {
        let validList = [];
        this.m_GraphUnitLst.forEach((p) => {
            if (p.m_graphType == LGXEnums.LGXGraphType.LineGraph_type && p.getSymbolLeadwireFlag()) {
                console.log("");
            }
            else {
                validList.push(p);
            }
        });

        let len = validList.length;
        if (len < 1) {
            return;
        }
        let firstObj = validList[0];
        this.m_symScope.left = firstObj._zone.left;
        this.m_symScope.right = firstObj._zone.right;
        this.m_symScope.top = firstObj._zone.top;
        this.m_symScope.bottom = firstObj._zone.bottom;

        for (let i = 1; i < len; i++) {
            let obj = validList[i];
            this.m_symScope.left = Math.min(this.m_symScope.left, obj._zone.left);
            this.m_symScope.right = Math.max(this.m_symScope.right, obj._zone.right);
            this.m_symScope.top = Math.max(this.m_symScope.top, obj._zone.top);
            this.m_symScope.bottom = Math.min(this.m_symScope.bottom, obj._zone.bottom);
        }

        let symCnt = this.m_symScope.getCnt();
        // validList.forEach((p) => {
        //     p._position.offset(-symCnt.x, -symCnt.y);
        // });

        firstObj = validList[0];
        firstObj.calScope(1);
        this.m_symScope.left = firstObj._zone.left;
        this.m_symScope.right = firstObj._zone.right;
        this.m_symScope.top = firstObj._zone.top;
        this.m_symScope.bottom = firstObj._zone.bottom;

        for (let i = 1; i < len; i++) {
            let obj = validList[i];
            obj.calScope(1);
            this.m_symScope.left = Math.min(this.m_symScope.left, obj._zone.left);
            this.m_symScope.right = Math.max(this.m_symScope.right, obj._zone.right);
            this.m_symScope.top = Math.max(this.m_symScope.top, obj._zone.top);
            this.m_symScope.bottom = Math.min(this.m_symScope.bottom, obj._zone.bottom);
        }

    }

    createUnitGraph(toolKind) {
        var factory = GraphFactory.GraphFactory;
        var pNewGraph = factory.createGraphByType32(toolKind, null);
        return pNewGraph;
    }

    getSymScope() {
        let rect = new LXCOM.LGXRect();
        rect.clone(this.m_symScope);
        return rect;
    }

    getLeadwireList() {
        let list = [];
        let len = this.m_symbolUnitLst.length;
        for (let i = 0; i < len; i++) {
            let obj = this.m_symbolUnitLst[i];
            var devFullType = obj.type;
            if (funcs.mainObjType(devFullType) == global.BaseUnitPolyline && obj.symbolLeadwireFlag) {
                list.push(obj);
            }
        }
        if (list.length == 0) {
            list = null;
        }
        return list;
    }

    //为图符中子图元也是图符的设置关联图符指针
    asignBlockUnitRefSymbol(symMng) {
        let len = this.m_GraphUnitLst.length;
        for (let i = 0; i < len; i++) {
            let unitGraph = this.m_GraphUnitLst[i];
            if (unitGraph.m_graphType == LGXEnums.LGXGraphType.MoldGraph_type) {
                let refSymbol = symMng.getSymbol(unitGraph.refSymbolUUID, 0);
                if (refSymbol) {
                    unitGraph.setSymbol(refSymbol);
                    unitGraph.calScope(1);
                }
                else {
                    console.log("");
                }
            }
        }
        this.calSymbolScope();
    }

    getMainFeaturePTLst(tFeaturePTLst, transformInfo) {
        let len = this.m_GraphUnitLst.length;
        for (let i = 0; i < len; i++) {
            let unitGraph = this.m_GraphUnitLst[i];
            unitGraph.getMainFeaturePTLst(tFeaturePTLst, transformInfo);
        }
    }

    getSubFeaturePTLst(tFeaturePTLst, mousePoint, transformInfo) {
        let len = this.m_GraphUnitLst.length;
        for (let i = 0; i < len; i++) {
            let unitGraph = this.m_GraphUnitLst[i];
            unitGraph.getSubFeaturePTLst(tFeaturePTLst,mousePoint, transformInfo);
        }
    }

    getState() { return this.m_state; }
    getTypeID() { return this.m_typeID; }
    getUUID() { return this.uuid; }
    getGraphUnitLst() { return this.m_GraphUnitLst; }
    getName() { return this.m_name; }
    setName(v) { this.m_name = v; }
    getSymbolUnitLst() { return this.m_symbolUnitLst; }
    setMoldActionType(v) { this.m_moldActionType = v; }
    getMoldActionType() { return this.m_moldActionType; }
    setAlgin2ItemBottomFlag(v) { this.m_algin2ItemBottom = v; }
    getAlgin2ItemBottomFlag() { return this.m_algin2ItemBottom; }
    setBeenRefFlag(v){this.beenRefFlag = v;}
    getBeenRefFlag(){return this.beenRefFlag;}
}

export default { LGXSymbol }